var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    endCampaginList: [],
    showBottom: false,
    loadMoreData:'~到底了喔~'
  },
  pageSize: 5,
  currentPageNum: 0,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var data = {}
    data.pageSize = that.pageSize
    util.request(api.EndCampaginList, data).then(function (success) {
      var endCampaginList = success.responseBody.campaignList

      console.log(success)
      that.setData({
        endCampaginList: endCampaginList
      });
    }, function (error) {
      console.log('获取推荐列表失败')
    })

  },
  
  onReachBottom: function() {
    console.log("触底了~~~")
    var that = this;
    var nowPageSize = that.pageSize + 5
    var currentPage = that.currentPageNum

    if (!that.data.showBottom) {
      wx.showLoading({
        title: '玩命加载中',
      })
      util.request(api.EndCampaginList, {
        pageSize: nowPageSize
      }).then(function(success) {

        console.log(success)
        var endCampaginList = success.responseBody.campaignList
        //推荐商品加载完毕，显示
        if (endCampaginList.length == that.currentPageNum || endCampaginList.length < 5) {
          that.setData({
            showBottom: true
          })
        }
        that.setData({
          endCampaginList: endCampaginList,
        });
        that.pageSize = that.pageSize + 5,
          that.currentPageNum = endCampaginList.length

        wx.hideLoading()

      }, function(error) {
        console.log('获取失败')
      })
    }
  },

  /**
   * 瀑布流商品点击事件
  */
  toRecommendDetail: function (e) {
    var that = this;
    app.currentGoodsId = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + app.currentGoodsId
    })
  }
})